﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.IO;

namespace S7.Windows.Menu
{
    /// <summary>
    /// Interaction logic for AddItem.xaml
    /// </summary>
    public partial class AddItem : Window
    {
        public AddItem()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    Menus.lstMenus = Menus.Search();

                    cbCategories.Dispatcher.Invoke(new ThreadStart(() =>
                        {
                            foreach (var item in Menus.lstMenus)
                            {
                                cbCategories.Items.Add(item.MenuName);
                            }
                        }), null);
                }));
            thr.IsBackground = true;
            try
            {
                thr.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFile = new System.Windows.Forms.OpenFileDialog();
            openFile.Multiselect = false;
            System.Windows.Forms.DialogResult dg = openFile.ShowDialog();
            if (dg == System.Windows.Forms.DialogResult.OK)
            {
                tbLocation.Text = openFile.FileName;
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string Name = Info.Capitalize(tbName.Text);
            string Price = Info.Capitalize(tbPrice.Text);
            string Desc = Info.Capitalize(tbDescription.Text);
            string pictureLocation = tbLocation.Text;
            if (cbCategories.SelectedItem != null)
            {
                string SelectedMenu = cbCategories.SelectedItem.ToString();
                Thread thr = new Thread(new ThreadStart(() =>
                {
                    if (Name != "" && Price != "")
                    {
                        var menuIDs = from menu in Menus.lstMenus
                                      where menu.MenuName == SelectedMenu
                                      select menu;
                        MenuItem item = new MenuItem();
                        item.MenuID = menuIDs.ToList()[0].MenuID;
                        item.MenuItemName = Name;
                        item.MenuItemPrice = decimal.Parse(Price);
                        item.MenuItemDescription = Desc;
                        item.Picture = pictureLocation;
                        string error = item.Add();
                        if (error == "")
                        {
                            MessageBox.Show("Menu Item is added", "Done!", MessageBoxButton.OK, MessageBoxImage.Information);
                            EditMenu.RefreshItems();
                            Application.Current.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                this.Close();
                            }), null);
                        }
                        else
                        {
                            MessageBox.Show(error, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please enter a name and a price", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }));
                thr.IsBackground = true;
                thr.Start();
            }
            else
            {
                MessageBox.Show("Please select a Menu", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Menus.lstMenus = null;
        }

    }
}
